<template>
  <basic-dialog
    :visible.sync="visible"
    :title="'修改报价有效期'"
    :width="'60%'"
    :handleClose="handleCloseDialog"
    :handleOk="handleSubmit"
    :loadingBtn="submitLoading"
    okText="提交"
  >
    <skus-table
      :showSelectCol="false"
      :showTotal="false"
      :dataSource="tableInfo.list"
      :columns="tableInfo.columns"
      :loading="tableInfo.loading"
      :tableHeight="400"
    >
      <el-row type="flex" justify="end" slot="content_context">
        <el-date-picker
          v-model="offerEndTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="'23:59:59'"
          :picker-options="pickerOptions"
          placeholder="请选择报价有效期"
        />
      </el-row>
    </skus-table>
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import SkusTable from '@/components/skusTable';
import skusPage from '@/mixins/commodity/skusPage';
import {
  editSupplierQuoteExpireTimeService,
} from '@/api/sourcing/public';

export default {
  name: 'EditExpireTime', // 修改报价有效期
  mixins: [skusPage],
  components: {
    BasicDialog,
    SkusTable,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    supplierList: {
      type: Array,
      required: true,
      default: () => {
        return []
      },
    },
  },
  data () {
    return {
      submitLoading: false,
      offerEndTime: '',
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() < new Date().getTime() - 86400000;
        }
      },
    };
  },

  computed: {
    // 需求id
    inquiryUuids () {
      return this.tableInfo.list.map(item => item.inquiryEntryId)
    },
  },
  watch: {
    supplierList: {
      handler (val) {
        this.tableInfo.list = val
        this.initSkuTableColumn()
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    initSkuTableColumn () {
      this.tableInfo.columns = [
        {
          key: 'supplierName',
          title: '供应商名称',
        },
        {
          key: 'supplierCode',
          title: '供应商代码',
        },
        {
          key: 'supplierLinkman',
          title: '供应商联系人',
        },
        {
          key: 'supplierPhone',
          title: '供应商电话',
        },
        {
          key: 'offerEndTime',
          title: '报价有效期-原值',
        },
      ]
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
    // 提交
    async handleSubmit () {
      if (!this.offerEndTime) {
        return this.$message.warning('请选择报价有效期')
      }
      const data = this.supplierList.map(item => item.sourcingSupplierId)
      const res = await editSupplierQuoteExpireTimeService({
        sourcingSupplierIds: data,
        offerEndTime: this.offerEndTime
      }, state => { this.submitLoading = state })
      if (res) {
        this.$message.success('提交成功')
        this.handleCloseDialog()
        this.$emit('getSupplierList')
      }
    },
  }
};
</script>
<style lang="less">
  .demand-card {
    margin-top: 10px;
  }
</style>
